package cn.rkylin.oms.goods.util;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import cn.rkylin.oms.goods.service.IGoodsService;
import cn.rkylin.oms.goods.util.handler.SnOpenableValidateHandler;
import cn.rkylin.oms.goods.util.handler.impl.AdjustValidHandler;
import cn.rkylin.oms.goods.util.handler.impl.AfterSaleResendValidHandler;
import cn.rkylin.oms.goods.util.handler.impl.OrderValidHandler;
import cn.rkylin.oms.goods.util.handler.impl.ReturnStockInValidHandler;
import cn.rkylin.oms.goods.util.handler.impl.ShipOrderValidHandler;
import cn.rkylin.oms.goods.util.handler.impl.StockInValidHandler;
import cn.rkylin.oms.goods.util.handler.impl.StockOutValidHandler;
import cn.rkylin.oms.goods.util.handler.impl.StockValidHandler;
import cn.rkylin.oms.goods.util.handler.impl.SysRefundValidHandler;
import cn.rkylin.oms.goods.util.handler.impl.SysReturnValidHandler;
import cn.rkylin.oms.goods.util.request.MyRequest;
import cn.rkylin.oms.goods.util.request.Request;
import cn.rkylin.oms.goods.vo.GoodsSkuVO;
import cn.rkylin.oms.goods.vo.GoodsVO;

@Component("snOpenableValidator")
public class SnOpenableValidator {
    @Autowired
    public IGoodsService goodsService;

    public SnOpenableValidator() {

    }
    
    /**
     * doValid:执行商品是否可以开启序列号管理的校验. <br/>
     *
     * @author zhanghao
     * @param goodsId 商品ID
     * @return  错误信息集合；如果校验通过，则没有错误信息
     * @throws Exception
     */
    @Transactional
    public String doValid(String goodsId) throws Exception {
        Request request = new MyRequest();
        GoodsVO goodsVO = goodsService.getGoodsById(goodsId);
        if(null==goodsVO) {
            return StringUtils.EMPTY;
        }
        // 获取商品的sku列表
        List<GoodsSkuVO> goodsSkuList = goodsService.getGoodsSkuByGoodsId(goodsId);
        if(goodsSkuList.size()==0) {
            return StringUtils.EMPTY;
        }
        // 构造request，执行校验
        request.setGoodsInfo(goodsVO);
        for (GoodsSkuVO goodsSkuVO : goodsSkuList) {
            request.addSkuCode(goodsSkuVO.getSkuCode());
        }
        
        SnOpenableValidateHandler orderValidHandler = new OrderValidHandler(); 
        SnOpenableValidateHandler stockValidHandler = new StockValidHandler();
        SnOpenableValidateHandler shipOrderValidHandler = new ShipOrderValidHandler();
        SnOpenableValidateHandler stockInValidHandler = new StockInValidHandler();
        SnOpenableValidateHandler stockOutValidHandler = new StockOutValidHandler();
        SnOpenableValidateHandler sysReturnValidHandler = new SysReturnValidHandler();
        SnOpenableValidateHandler returnStockInValidHandler = new ReturnStockInValidHandler();
        SnOpenableValidateHandler adjustValidHandler = new AdjustValidHandler();
        SnOpenableValidateHandler afterSaleResendValidHandler = new AfterSaleResendValidHandler();
        SnOpenableValidateHandler sysReFundValidHandler = new SysRefundValidHandler();
        
        // 构建责任链（库存-订单-发货单-入库单-出库单-退货单-退货入库单-调整单-补发单-退款单）
        stockValidHandler.setSuccessor(orderValidHandler);
        orderValidHandler.setSuccessor(shipOrderValidHandler);
        shipOrderValidHandler.setSuccessor(stockInValidHandler);
        stockInValidHandler.setSuccessor(stockOutValidHandler);
        stockOutValidHandler.setSuccessor(sysReturnValidHandler);
        sysReturnValidHandler.setSuccessor(returnStockInValidHandler);
        returnStockInValidHandler.setSuccessor(adjustValidHandler);
        adjustValidHandler.setSuccessor(afterSaleResendValidHandler);
        afterSaleResendValidHandler.setSuccessor(sysReFundValidHandler);
        
        // 执行校验
        List<String> errMsgList = stockValidHandler.handleRequest(request);
        String returnErrorMsg= "";
        for (String string : errMsgList) {
            returnErrorMsg = returnErrorMsg.concat(string)+";";
        }
        
        return returnErrorMsg;
    }
}
